<?php

namespace App\Http\Controllers;

use App\Http\Logic\Api\AuditToken;
use Exception;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    protected function success($data=[], string $msg = '成功'): JsonResponse
    {
        return response()->json(
            [
                'code'=>200,
                'msg'=>$msg,
                'data'=>$data
            ]
        );
    }

    protected function error(string $msg = '失败', array $data = []): JsonResponse
    {
        return response()->json(
            [
                'code'=>400,
                'msg'=>$msg,
                'data'=>$data
            ]
        );
    }

    /**
     * @throws Exception
     */
    public function getUser()
    {
        return AuditToken::decryptToken(str_replace('Bearer ', '', request()->headers->get('authorization')));
    }
}
